//
//  TraditionSecPOSVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/1/2.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "TraditionSecPOSVC.h"
#import "addAdressCell.h"
#import "ShopBigTypeView.h"
#import "BusinessShopflv.h"
#import "POSSureViewController.h"
#import "ScanQRViewController.h"
#import "ZHFAddTitleAddressView.h"
#import "MinedoPosStore.h"
#import "BusinessManagerStore.h"

@interface TraditionSecPOSVC ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ZHFAddTitleAddressViewDelegate,ScanQRViewControllerDelegate>

@property (nonatomic, strong) ZHFAddTitleAddressView *addTitleAddressView;
@property (nonatomic, strong) ShopBigTypeView *SubShoptypeView;
@property (nonatomic, strong) BusinessShopflv *ShopflvView;
@property (nonatomic, strong) UITableView *tableView;


@property (nonatomic, copy) NSString *merchantName;
@property (nonatomic, copy) NSString *merchantcard;
@property (nonatomic, copy) NSString *merchantbank;
@property (nonatomic, copy) NSString *merhbankname;

@property (nonatomic, strong) UIImage *POSidFontImage;
@property (nonatomic, strong) UIImage *POSidBackImage;
@property (nonatomic, strong) UIImage *Bankimage;
@property (nonatomic, strong) UIImage *BankBackImage;
@property (nonatomic, strong) UIImage *holdImage;
@property (nonatomic, strong) UIImage *BusinessLicense;
@property (nonatomic, strong) UIImage *doorimg;
@property (nonatomic, strong) UIImage *mentimg;

@property (nonatomic, copy) NSString *phoneName;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *realCode;
@property (nonatomic, copy) NSString *posNnstanceSn;
@property (nonatomic, copy) NSString *qqemail;

@property (nonatomic, copy) NSString *addressDetail;
@property (nonatomic, copy) NSString *addresstit;
@property (nonatomic, copy) NSString *banktit;
@property (nonatomic, copy) NSString *shoptype;
@property (nonatomic, copy) NSString *shopflv;
@property (nonatomic, copy) NSString *region;

@property (nonatomic, assign) NSInteger buttontag;
@end

@implementation TraditionSecPOSVC

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:YES];
//    self.navigationController.navigationBar.hidden = YES;
//}
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:YES];
//    self.navigationController.navigationBar.hidden = NO;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self setUI];
    [self ViewWithTableview];
    [self ViewWithShopflv];
    [self getWithlevelData];
}
- (void)getWithlevelData{
    [[[MinedoPosStore alloc]init] GetLevelWithPrams:@{@"customId":[UserDefaultsManager sharedInstance].user_id,@"isNew":@"1"} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.ShopflvView.levelstr = res[@"data"][@"levelAmount"];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)initCSS{
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    self.buttontag = 0;
    
    [self setNavBarWithTitle:@"传统POS开通认证"];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.bottom.equalTo(self.view);
    }];
}
- (void)setUI{
    self.addTitleAddressView = [[ZHFAddTitleAddressView alloc]init];
    self.addTitleAddressView.title = @"选择地址";
    self.addTitleAddressView.userID = 7;
    self.addTitleAddressView.delegate1 = self;
    self.addTitleAddressView.defaultHeight = 350;
    self.addTitleAddressView.titleScrollViewH = 37;
    [self.view addSubview:[self.addTitleAddressView initAddressView]];
}
- (void)ViewWithTableview{
    WS(weakself)
    self.SubShoptypeView = [[ShopBigTypeView alloc]init];
    [self.SubShoptypeView setGetWithinfoBlock:^(NSString * substring) {
        weakself.shoptype = substring;
        [weakself.tableView reloadData];
    }];
    [self.view addSubview:self.SubShoptypeView];
}
- (void)ViewWithShopflv{
    WS(weakself)
    self.ShopflvView = [[BusinessShopflv alloc]init];
    [self.ShopflvView setGetWithFlvBlock:^(NSString * substring) {
        weakself.shopflv = substring;
        [weakself.tableView reloadData];
    }];
    [self.view addSubview:self.ShopflvView];
    
}
- (void)CliktoSure{
    NSMutableDictionary *prams = [[NSMutableDictionary alloc]init];
    [prams setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"customId"];
    if (self.merchantName.length > 0) {
        [prams setObject:self.merchantName forKey:@"merchantName"];
        
    if (self.merchantcard.length > 0) {
        [prams setObject:self.merchantcard forKey:@"idCard"];
            
            
    if (self.merchantbank.length > 0) {
        [prams setObject:self.merchantbank forKey:@"bankCardNumber"];
                
    if (self.merhbankname.length > 0) {
        [prams setObject:self.merhbankname forKey:@"bank"];
                    
    if (self.addresstit.length > 0) {
        [prams setObject:self.region forKey:@"region"];
                        
    if (self.addressDetail.length > 0) {
        [prams setObject:self.addressDetail forKey:@"address"];
                            
    if (self.banktit.length > 0) {
        [prams setObject:self.banktit forKey:@"khzh"];
                                
    if (self.shoptype.length > 0) {
        [prams setObject:self.shoptype forKey:@"businessType"];
                                    
    if (self.shopflv.length > 0) {
        [prams setObject:[self.shopflv substringToIndex:self.shopflv.length - 1] forKey:@"rate"];
    }else{
        [prams setObject:@"0.6" forKey:@"rate"];
    }
                                    
    if ([self.phoneName isVaildMobileNo]) {
        [prams setObject:self.phoneName forKey:@"phone"];
                                        
    if (self.code.length > 0) {
    if ([self.code isEqualToString:self.realCode]) {
        
    if (self.qqemail.length > 0) {
        [prams setObject:self.qqemail forKey:@"qqemail"];
        
    if (self.posNnstanceSn.length > 0) {
        [prams setObject:self.posNnstanceSn forKey:@"sn"];
                                                        
                                                        
    if (self.POSidFontImage) {
        [prams setObject:[ImgManager imageToString:self.POSidFontImage] forKey:@"idCardFront"];

    if (self.POSidBackImage) {
        [prams setObject:[ImgManager imageToString:self.POSidBackImage] forKey:@"idCardBack"];

    if (self.holdImage) {
        [prams setObject:[ImgManager imageToString:self.holdImage] forKey:@"holdIdCardFront"];
    
    if (self.Bankimage) {
        [prams setObject:[ImgManager imageToString:self.Bankimage] forKey:@"bankFont"];
    if (self.BankBackImage) {
        [prams setObject:[ImgManager imageToString:self.BankBackImage] forKey:@"bankBack"];
        
        if (self.BusinessLicense) {
            [prams setObject:[ImgManager imageToString:self.BusinessLicense] forKey:@"businessLicense"];
            if (self.doorimg) {
                [prams setObject:[ImgManager imageToString:self.doorimg] forKey:@"door"];
                if (self.mentimg) {
                    [prams setObject:[ImgManager imageToString:self.mentimg] forKey:@"businessment"];
                    [self loadAddData:prams];
                }else{
                    [self showMBPError:@"未上传营业环境照"];
                }
            }else{
                [self showMBPError:@"未上传门头照"];
            }
        }else{
            [self showMBPError:@"未获取到营业执照"];
        }
            
    }else{
        [self showMBPError:@"未获取到银行卡背面照"];
    }
        
    }else{
        [self showMBPError:@"未获取到银行卡正面照"];
    }
                                                                    
    }else{
        [self showMBPError:@"未获取到手持照"];
    }
                                                                
    }else{
        [self showMBPError:@"未获取到身份证正面照"];
    }
                                                            
    }else{
        [self showMBPError:@"未获取到身份证正面照"];
    }
                                                        
    }else{
        [self showMBPError:@"请输入机具编号"];
    }
                                                    
    }else{
        [self showMBPError:@"请输入QQ邮箱"];
    }
    }else{
        [self showMBPError:@"请输入正确验证码"];
    }
    }else{
        [self showMBPError:@"请输入验证码"];
    }
    }else{
        [self showMBPError:@"未填写商户电话"];
    }
                                    
    }else{
        [self showMBPError:@"未填写商户类型"];
    }
                                
    }else{
        [self showMBPError:@"未填写开户银行"];
    }
                            
    }else{
        [self showMBPError:@"请输入装机地址"];
    }
                        
    }else{
        [self showMBPError:@"未填写所属地区"];
    }
                    
    }else{
        [self showMBPError:@"未获取到结算银行"];
    }
                
    }else{
        [self showMBPError:@"未获取到银行卡号"];
    }
            
    }else{
        [self showMBPError:@"未获取到身份证号"];
    }
        
    }else{
        [self showMBPError:@"未获取到商户姓名"];
    }
}
- (void)loadAddData:(NSDictionary *)prams{
    WS(weakSelf);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[[BusinessManagerStore alloc]init] PostAddPOSWithPrams:prams Success:^(NSDictionary * _Nonnull res) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if ([res[@"msg"] isEqualToString:@"success"]) {
            [SVProgressHUD dismiss];
            POSSureViewController *VC = [[POSSureViewController alloc]init];
            VC.StatusType = 0;
            [[AppDelegate sharedAppDelegate] pushViewController:VC];
        }else{
            [SVPManager showErrorWithTitle:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)sendCodeWith:(NSIndexPath *)index{
    [[[MinedoPosStore alloc]init] sendCodeWithTelephoneNumber:self.phoneName Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.realCode = res[@"data"][@"code"];
            addAdressCell *Cell = [self.tableView cellForRowAtIndexPath:index];
            [Cell daojishi];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
#pragma mark - ZHFAddTitleAddressViewDelegate
-(void)cancelBtnClick:(NSString *)titleAddress titleID:(NSString *)titleID{
    if (titleID.length <= 0) {
        return;
    }
    self.region = titleID;
    self.addresstit = titleAddress;
    [self.tableView reloadData];
}

#pragma mark - scan QR code delegate
- (void)didScanSuccessWithResult:(NSString *)result{
    // 先将服务端返回的 URL 转义
    NSString *decodedString = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.posNnstanceSn = decodedString;
    [self.tableView reloadData];
}

#pragma mark - 相册选择完成
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        if (self.buttontag - 3453 == 0) {
            self.POSidFontImage = image;
        }
        if (self.buttontag - 3453 == 1) {
            self.POSidBackImage = image;
        }
        if (self.buttontag - 3453 == 2) {
            self.Bankimage = image;
        }
        if (self.buttontag - 3453 == 3) {
            self.BankBackImage = image;
        }
        if (self.buttontag - 3453 == 4) {
            self.holdImage = image;
        }
        if (self.buttontag - 3453 == 5) {
            self.BusinessLicense = image;
        }
        if (self.buttontag - 3453 == 6) {
            self.doorimg = image;
        }
        if (self.buttontag - 3453 == 7) {
            self.mentimg = image;
        }
        [self.tableView reloadData];
    }];
}
-(void)textFieldDidEditing:(UITextField *)textField{
    if (textField.tag - 4433 == 2) {
        self.banktit = textField.text;
    }
    if (textField.tag - 4433 == 3) {
        self.addresstit = textField.text;
    }
    if (textField.tag - 4433 == 4) {
        self.addressDetail = textField.text;
    }
    if (textField.tag - 4433 == 5) {
        self.shoptype = textField.text;
    }
}
#define TableNumber  10
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return TableNumber + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 1){
        return 2;
    }else if (section == 7){
        return 4;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 8){
        return 392;
    }else if (indexPath.section == 9){
        return 473;
    }else if (indexPath.section == 10){
        return (SCREEN_WIDTH - 58) / 2;
    }else{
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2 || section == 3 || section == 4 || section == 5 || section == 6){
        return 32;
    }else{
        return 10;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 2  || section == 3 || section == 4 || section == 5 || section == 6){
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 32)];
        header.backgroundColor = [UIColor clearColor];
        UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(14, 6, 100, 20)];
        subtitle.textColor = [UIColor colorWithHexString:@"333333"];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
        [header addSubview:subtitle];
        if (section == 2) {
            subtitle.text = @"结算开户行";
        }
        if (section == 3) {
            subtitle.text = @"所属省市";
        }
        if (section == 4) {
            subtitle.text = @"装机地址";
        }
        if (section == 5) {
            subtitle.text = @"商户类型";
        }
        if (section == 6) {
            subtitle.text = @"商户费率设定";
        }
        return header;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 10) {
        return 74;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 10) {
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 74)];
        header.backgroundColor = [UIColor clearColor];
        UIButton *sure = [[UIButton alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 49)];
        sure.backgroundColor = [UIColor colorWithHexString:@"0CA6F0"];
        [sure setTitle:@"确认并提交" forState:UIControlStateNormal];
        sure.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:18];
        [sure setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
        [sure addTarget:self action:@selector(CliktoSure) forControlEvents:UIControlEventTouchUpInside];
        [header addSubview:sure];
        return header;
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 4 || indexPath.section == 5 || indexPath.section == 6) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        UITextField *title = [[UITextField alloc]init];
        title.tag = 4433 + indexPath.section;
        [title addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
        title.textColor = [UIColor colorWithHexString:@"333333"];
        title.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
        [cell.contentView addSubview:title];
        UIImageView *icon = [[UIImageView alloc]init];
        icon.image = [UIImage imageNamed:@"灰色右箭头"];
        [cell.contentView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(13);
            make.width.mas_equalTo(7);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            make.right.equalTo(cell.contentView.mas_right).offset(-14);
        }];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView.mas_centerY);
            make.left.equalTo(cell.contentView.mas_left).offset(14);
            make.right.equalTo(icon.mas_left).offset(-20);
            make.height.mas_equalTo(40);
        }];
        if (indexPath.section == 2) {
            title.enabled = YES;
            icon.hidden = YES;
            if (self.banktit) {
                if (self.banktit.length > 0) {
                    title.text = self.banktit;
                }else{
                    title.placeholder = @"请选择开户银行";
                }
            }else{
                title.placeholder = @"请选择开户银行";
            }
        }else if (indexPath.section == 3) {
            title.enabled = NO;
            icon.hidden = NO;
            if (self.addresstit) {
                if (self.addresstit.length > 0) {
                    title.text = self.addresstit;
                }else{
                    title.placeholder = @"请选择地区";
                }
            }else{
                title.placeholder = @"请选择地区";
            }
        }else if (indexPath.section == 4) {
            title.enabled = YES;
            icon.hidden = YES;
            if (self.addressDetail) {
                if (self.addressDetail.length > 0) {
                    title.text = self.addressDetail;
                }else{
                    title.placeholder = @"请输入装机地址";
                }
            }else{
                title.placeholder = @"请输入装机地址";
            }
        }else if (indexPath.section == 5) {
            title.enabled = NO;
            icon.hidden = NO;
            if (self.shoptype) {
                if (self.shoptype.length > 0) {
                    title.text = self.shoptype;
                }else{
                    title.placeholder = @"请选择商户类型";
                }
            }else{
                title.placeholder = @"请选择商户类型";
            }
        }else if (indexPath.section == 6) {
            title.enabled = NO;
            icon.hidden = NO;
            if (self.shopflv) {
                if (self.shopflv.length > 0) {
                    title.text = [NSString stringWithFormat:@"当前费率%@",self.shopflv];
                }else{
                    title.placeholder = @"当前费率0.60%";
                }
            }else{
                title.placeholder = @"当前费率0.60%";
            }
        }
    }else if (indexPath.section == 7) {
        [self.tableView registerNib:[UINib nibWithNibName:@"addAdressCell" bundle:nil] forCellReuseIdentifier:@"addAdressCellID2"];
        addAdressCell *GCell = [tableView dequeueReusableCellWithIdentifier:@"addAdressCellID2"];
        GCell.rightarrow.hidden = YES;
        GCell.sendbutton.hidden = YES;
        GCell.detailtext.enabled = YES;
        GCell.subtitle.text = @"";
        GCell.detailtext.text = @"";
        GCell.detailtext.textColor = [UIColor colorWithHexString:@"333333"];
        if (indexPath.row == 0) {
            GCell.subtitle.text = @"商户电话";
            if (self.phoneName) {
                if (self.phoneName.length > 0) {
                    GCell.detailtext.text = self.phoneName;
                }else{
                    GCell.detailtext.placeholder = @"输入手机号码";
                }
            }else{
                GCell.detailtext.placeholder = @"输入手机号码";
            }
        }else if (indexPath.row == 1) {
            GCell.subtitle.text = @"验证码";
            if (self.code) {
                if (self.code.length > 0) {
                    GCell.detailtext.text = self.code;
                }else{
                    GCell.detailtext.placeholder = @"输入验证码";
                }
            }else{
                GCell.detailtext.placeholder = @"输入验证码";
            }
            GCell.sendbutton.hidden = NO;
            [GCell.sendbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [GCell setSendBlock:^{
                if ([self.phoneName isVaildMobileNo]) {
                    [self sendCodeWith:indexPath];
                }else{
                    [self showMBPError:@"请输入正确的手机号"];
                }
            }];
        }else if (indexPath.row == 2) {
            GCell.subtitle.text = @"邮箱";
            if (self.qqemail) {
                if (self.qqemail.length > 0) {
                    GCell.detailtext.text = self.qqemail;
                }else{
                    GCell.detailtext.placeholder = @"建议输入QQ邮箱";
                }
            }else{
                GCell.detailtext.placeholder = @"建议输入QQ邮箱";
            }
        }else if (indexPath.row == 3) {
            GCell.subtitle.text = @"机具编号";
            if (self.posNnstanceSn) {
                if (self.posNnstanceSn.length > 0) {
                    GCell.detailtext.text = self.posNnstanceSn;
                }else{
                    GCell.detailtext.placeholder = @"输入机器序列号(SN)";
                }
            }else{
                GCell.detailtext.placeholder = @"输入机器序列号(SN)";
            }
            GCell.sendbutton.hidden = NO;
            [GCell.sendbutton setTitle:@"自动识别" forState:UIControlStateNormal];
        }
        [GCell setDetailBlock:^(NSString * _Nonnull str) {
            if (indexPath.row == 0) {
                self.phoneName = str;
            }else if (indexPath.row == 1) {
                self.code = str;
            }else if (indexPath.row == 2) {
                self.qqemail = str;
            }else if (indexPath.row == 3) {
                self.posNnstanceSn = str;
            }
        }];
        cell = GCell;
    }else if (indexPath.section == 8) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
        cell.backgroundColor = [UIColor clearColor];
        UIView *imagebg = [[UIView alloc]init];
        imagebg.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:imagebg];
        [imagebg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.mas_top).offset(15);
            make.left.equalTo(cell.mas_left).offset(14);
            make.right.equalTo(cell.mas_right).offset(-14);
            make.bottom.equalTo(cell.mas_bottom);
        }];
        CGFloat width = (SCREEN_WIDTH - 109)/2;
        
        for (NSInteger i = 0; i < 5; i++) {
            UIButton *chooseimage = [[UIButton alloc]init];
            chooseimage.layer.masksToBounds = YES;
            chooseimage.layer.cornerRadius = 3;
            chooseimage.tag = 3453 + i;
            [chooseimage addTarget:self action:@selector(selectWithImg:) forControlEvents:UIControlEventTouchUpInside];
            [imagebg addSubview:chooseimage];
            [chooseimage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(85);
                make.width.mas_equalTo(width);
                make.top.equalTo(imagebg.mas_top).offset(19 + 117 * (i / 2));
                make.left.equalTo(imagebg.mas_left).offset(27 + (width + 27) * (i % 2));
            }];
            UILabel *titlelab = [[UILabel alloc]init];
            titlelab.textColor = [UIColor colorWithHexString:@"333333"];
            titlelab.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
            [imagebg addSubview:titlelab];
            [titlelab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(chooseimage.mas_centerX);
                make.top.equalTo(chooseimage.mas_bottom).offset(10);
                make.height.mas_equalTo(12);
            }];
            if (i == 0) {
                titlelab.text = @"身份证正面";
                if (self.POSidFontImage) {
                    [chooseimage setImage:self.POSidFontImage forState:UIControlStateNormal];
                }else{
                    [chooseimage setImage:[UIImage imageNamed:@"手填身份证正面"] forState:UIControlStateNormal];
                }
            }
            if (i == 1) {
                titlelab.text = @"身份证反面";
                if (self.POSidBackImage) {
                    [chooseimage setImage:self.POSidBackImage forState:UIControlStateNormal];
                }else{
                    [chooseimage setImage:[UIImage imageNamed:@"手填身份证背面"] forState:UIControlStateNormal];
                }
            }
            if (i == 2) {
                titlelab.text = @"银行卡正面";
                if (self.Bankimage) {
                    [chooseimage setImage:self.Bankimage forState:UIControlStateNormal];
                }else{
                    [chooseimage setImage:[UIImage imageNamed:@"手填交易卡正面"] forState:UIControlStateNormal];
                }
            }
            if (i == 3) {
                titlelab.text = @"银行卡背面";
                if (self.BankBackImage) {
                    [chooseimage setImage:self.BankBackImage forState:UIControlStateNormal];
                }else{
                    [chooseimage setImage:[UIImage imageNamed:@"手填交易卡背面"] forState:UIControlStateNormal];
                }
            }
            if (i == 4) {
                titlelab.text = @"手持身份证照片";
                if (self.holdImage) {
                    [chooseimage setImage:self.holdImage forState:UIControlStateNormal];
                }else{
                    [chooseimage setImage:[UIImage imageNamed:@"手填手持身份证"] forState:UIControlStateNormal];
                }
            }
        }
    }else if (indexPath.section == 9){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell4"];
        UILabel *titlelab = [[UILabel alloc]init];
        titlelab.textColor = [UIColor colorWithHexString:@"333333"];
        titlelab.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
        titlelab.text = @"营业执照照片";
        [cell.contentView addSubview:titlelab];
        [titlelab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView.mas_left).offset(14);
            make.top.equalTo(cell.contentView.mas_top).offset(10);
        }];
        UILabel *subtitle = [[UILabel alloc]init];
        subtitle.textColor = [UIColor colorWithHexString:@"999999"];
        subtitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
        subtitle.text = @"（必填）";
        [cell.contentView addSubview:subtitle];
        [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titlelab.mas_centerY);
            make.left.equalTo(titlelab.mas_right);
        }];
        UIImageView *image = [[UIImageView alloc]init];
        if (self.BusinessLicense) {
            image.image = self.BusinessLicense;
        }else{
            image.image = [UIImage imageNamed:@"默认营业执照"];
        }
        [cell.contentView addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titlelab.mas_bottom).offset(20);
            make.left.equalTo(cell.contentView.mas_left).offset(50);
            make.right.equalTo(cell.contentView.mas_right).offset(-50);
            make.bottom.equalTo(cell.contentView.mas_bottom).offset(-20);
        }];
    }else if (indexPath.section == 10){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell8"];
        UILabel *submentou = [[UILabel alloc]init];
        submentou.textColor = [UIColor colorWithHexString:@"999999"];
        submentou.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
        submentou.text = @"门头照（必填）";
        [cell.contentView addSubview:submentou];
        [submentou mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.contentView.mas_top).offset(4);
            make.left.equalTo(cell.contentView.mas_left).offset(14);
            make.height.mas_equalTo(14);
        }];
        
        UIButton *doorimg = [[UIButton alloc]init];
        doorimg.tag = 3459;
        doorimg.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
        if (self.doorimg) {
            [doorimg setImage:self.doorimg forState:UIControlStateNormal];
        }else{
            [doorimg setImage:[UIImage imageNamed:@"默认加号底图"] forState:UIControlStateNormal];
        }
        [doorimg addTarget:self action:@selector(selectWithImg:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:doorimg];
        [doorimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView.mas_left).offset(15);
            make.top.equalTo(submentou.mas_bottom).offset(4);
            make.bottom.equalTo(cell.contentView.mas_bottom).offset(-5);
            make.width.mas_equalTo((SCREEN_WIDTH - 40)/2);
        }];
        
        UILabel *subhuanjin = [[UILabel alloc]init];
        subhuanjin.textColor = [UIColor colorWithHexString:@"999999"];
        subhuanjin.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
        subhuanjin.text = @"营业环境照（必填）";
        [cell.contentView addSubview:subhuanjin];
        [subhuanjin mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(submentou.mas_centerY);
            make.left.equalTo(doorimg.mas_right).offset(10);
            make.height.mas_equalTo(14);
        }];
        
        UIButton *mentimg = [[UIButton alloc]init];
        mentimg.tag = 3460;
        mentimg.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
        if (self.mentimg) {
            [mentimg setImage:self.mentimg forState:UIControlStateNormal];
        }else{
            [mentimg setImage:[UIImage imageNamed:@"默认加号底图"] forState:UIControlStateNormal];
        }
        [mentimg addTarget:self action:@selector(selectWithImg:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:mentimg];
        [mentimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(doorimg.mas_right).offset(10);
            make.top.equalTo(subhuanjin.mas_bottom).offset(4);
            make.bottom.equalTo(cell.contentView.mas_bottom).offset(-5);
            make.width.mas_equalTo((SCREEN_WIDTH - 40)/2);
        }];
    }else if(indexPath.section == 0 || indexPath.section == 1){
        [self.tableView registerNib:[UINib nibWithNibName:@"addAdressCell" bundle:nil] forCellReuseIdentifier:@"addAdressCellID"];
        addAdressCell *DCell = [tableView dequeueReusableCellWithIdentifier:@"addAdressCellID"];
        DCell.rightarrow.hidden = YES;
        DCell.detailtext.textColor = [UIColor colorWithHexString:@"333333"];
        DCell.detailtext.enabled = YES;
        DCell.sendbutton.hidden = YES;
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                DCell.subtitle.text = @"商户姓名";
                DCell.detailtext.placeholder = @"请输入商户姓名";
            }else if (indexPath.row == 1) {
                DCell.subtitle.text = @"身份证号";
                DCell.detailtext.placeholder = @"请输入身份证号码";
            }
        }
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                DCell.subtitle.text = @"结算卡号";
                DCell.detailtext.placeholder = @"请输入银行卡号";
            }else if (indexPath.row == 1) {
                DCell.subtitle.text = @"结算银行";
                DCell.detailtext.placeholder = @"请输入结算银行";
            }
        }
        [DCell setDetailBlock:^(NSString * _Nonnull str) {
            if (indexPath.section == 0) {
                if (indexPath.row == 0) {
                    self.merchantName = str;
                }else if (indexPath.row == 1) {
                    self.merchantcard = str;
                }
            }
            if (indexPath.section == 1) {
                if (indexPath.row == 0) {
                    self.merchantbank = str;
                }else if (indexPath.row == 1) {
                    self.merhbankname = str;
                }
            }
        }];
        cell = DCell;
    }
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell5"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        [self.addTitleAddressView addAnimatetype2];
    }else if (indexPath.section == 5) {
        [self.SubShoptypeView ViewDidPush];
    }else if (indexPath.section == 6) {
        [self.ShopflvView ViewDidPush];
    }else if (indexPath.section == 9) {
        self.buttontag = 3458;
        [self selectImg];
    }
}
- (void)selectWithImg:(UIButton *)button{
    self.buttontag = button.tag;
    [self selectImg];
}
#pragma mark - 选择图片
- (void)selectImg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *phone = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }];
    [alert addAction:phone];
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
    }else{
        UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            [self presentViewController:picker animated:YES completion:nil];
        }];
        [alert addAction:camera];
    }
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancle];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
